package cn.szzsi._admin;

import cn.szzsi.common.model.AreaSystem;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

public class AreaSystemController extends BaseAdminController<AreaSystem> {

    /**
     * 所有地区列表
     */
    public void list(){
        String parentId =getPara("parentId");
        List<AreaSystem> areaSystemList= AreaSystem.dao.getByParent(parentId);
        if(StrKit.isBlank(parentId)){
            setAttr("parent",null);
        }else{
            AreaSystem areaSystem= AreaSystem.dao.findById(parentId);
            setAttr("parent",areaSystem);
        }
        setAttr("areaSystemList",areaSystemList);
        render("area_system_list.html");
    }

    /**
     * 编辑
     */
    public void edit(){
        String id=getPara("id");
        AreaSystem areaSystem= AreaSystem.dao.findById(id);
        if(areaSystem.getParent()==null){
            setAttr("parent",null);
        }else{
            AreaSystem parent= AreaSystem.dao.findById(areaSystem.getParent());
            setAttr("parent",parent);
        }
        setAttr("areaSystem",areaSystem);
        render("area_system_edit.html");

    }

    /**
     * 更新
     */
    public void update(){
        AreaSystem areaSystem=getModel(AreaSystem.class);
        areaSystem.setModifyDate(new Date());
        String url="list";

        //该区域更改之前的对象
        AreaSystem previousAreaSystem= AreaSystem.dao.findById(areaSystem.getId());
        String previousName=previousAreaSystem.getName();
        //该区域更改之前的对象

        //将所有和本级关联的fullName全部改为本次更改后的全称
        List<AreaSystem>relationAreaSystemList= AreaSystem.dao.find("select * from mall_area_system where id=? or tree_path like '%"+areaSystem.getId()+"%'",areaSystem.getId());
        for(AreaSystem relationAreaSystem:relationAreaSystemList){
            String relationFullName=relationAreaSystem.getFullName();
            String fullName=StringUtils.replace(relationFullName,previousName,areaSystem.getName());
            relationAreaSystem.setFullName(fullName);
            relationAreaSystem.update();
        }
        if(areaSystem.getParent()!=null){
            url="list?parentId="+areaSystem.getParent();
        }
        areaSystem.update();
        renderJson(Ret.ok("info","修改成功!").set("url",url));
    }


    //新增
    public void add(){
        String parentId=getPara("parentId");
        AreaSystem parentAreaSystem= AreaSystem.dao.findById(parentId);
        setAttr("parentAreaSystem",parentAreaSystem);
        setAttr("parentId",parentId);
        render("area_system_add.html");
    }

    //保存
    public void save(){
        AreaSystem areaSystem=getModel(AreaSystem.class);
        areaSystem.setCreateDate(new Date());
        areaSystem.setModifyDate(new Date());
        String url="list";
        if(areaSystem.getParent()!=null){//含有上级,查询上级信息
            AreaSystem parentAreaSystem= AreaSystem.dao.findById(areaSystem.getParent());
            //String parentName=parentAreaSystem.getName();
            String parentFullName=parentAreaSystem.getFullName();
            String parentTreePath=parentAreaSystem.getTreePath();
            areaSystem.setFullName(parentFullName+areaSystem.getName());
            areaSystem.setTreePath(parentTreePath+parentAreaSystem.getId()+",");
            url="list?parentId="+areaSystem.getParent();
        }else{
            areaSystem.setTreePath(",");
            areaSystem.setFullName(areaSystem.getName());
        }
        areaSystem.save();
        renderJson(Ret.ok("info","添加成功!").set("url",url));
    }

    public void delete(){
        String id=getPara("id");

        AreaSystem areaSystem= AreaSystem.dao.findById(id);
        if(areaSystem==null){
            renderJson(Ret.fail("info","区域不存在"));
            return;
        }
        //1.删除关联下级的所有区域
        Db.update("delete from mall_area_system where tree_path like '%"+id+"%'");
        //2.删除该区域
        areaSystem.deleteById(id);
        renderJson(Ret.ok("info","删除成功"));
    }

}
